// #include "../common/message.hpp"
// #include "../common/net.hpp"
// #include "../common/dispatcher.hpp"
// #include "../client/rpc_caller.hpp"

// #include <thread>

// void onRpcResponse(const RPC::BaseConnection::ptr& conn, RPC::RpcResponse::ptr& msg)
// {
//     std::cout << "收到了Rpc请求: " << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }

// void onTopicResponse(const RPC::BaseConnection::ptr& conn, RPC::TopicResponse::ptr& msg)
// {
//     std::cout << "收到了Topic请求: " << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }

#include "../../client/rpc_client.hpp"
#include "../../common/detail.hpp"

int main()
{
    
    // auto requestor = std::make_shared<RPC::client::Requestor>();
    // auto caller = std::make_shared<RPC::client::RpcCaller>(requestor);


    // auto dispatcher = std::make_shared<RPC::Dispatcher>();
    // auto rsp_cb = std::bind(&RPC::client::Requestor::onResponse, requestor.get(), 
    //         std::placeholders::_1, std::placeholders::_2);
    // dispatcher->registerHandler<RPC::BaseMessage>(RPC::MType::RSP_RPC, rsp_cb);
    // //dispatcher->registerHandler<RPC::TopicResponse>(RPC::MType::RSP_TOPIC, onTopicResponse);


    // auto client = RPC::ClientFactory::create("127.0.0.1", 9090);
    // auto message_cb = std::bind(&RPC::Dispatcher::onMessage, dispatcher.get(), 
    //         std::placeholders::_1, std::placeholders::_2);
    // client->setMessageCallback(message_cb);
    // client->connect();

    // auto conn = client->connection();

    RPC::client::RpcClient client(false, "127.0.0.1", 9090);
    Json::Value params, result;
    params["num1"] = 11;
    params["num2"] = 22;
    bool ret = client.call("Add", params, result);
    if(ret == false)
    {
        ILOG("result: %d", result.asInt());
    }

    RPC::client::RpcCaller::JsonAsyncResponse res_future;
    params["num1"] = 33;
    params["num2"] = 44;
    ret = client.call("Add", params, result);
    if(ret == false)
    {
        result = res_future.get();
        ILOG("result: %d", result.asInt());
    }

    params["num1"] = 55;
    params["num2"] = 66;
    ret = client.call("Add", params, result);
    DLOG("..........\n");
    std::this_thread::sleep_for(std::chrono::seconds(1));

    // auto rpc_req = RPC::MessageFactory::create<RPC::RpcRequest>();
    // rpc_req->setId("666");
    // rpc_req->setMType(RPC::MType::REQ_RPC);
    // rpc_req->setMethod("Add");
    // Json::Value val;
    // val["num1"] = 11;
    // val["num2"] = 22;
    // rpc_req->setparams(val);
    // client->send(rpc_req);

    // auto topic_req = RPC::MessageFactory::create<RPC::TopicRequest>();
    // topic_req->setId("2222");
    // topic_req->setMType(RPC::MType::REQ_TOPIC);
    // topic_req->setOptype(RPC::TopicOpType::TOPIC_CREATE);
    // topic_req->setTopicKey("news");
    // client->send(topic_req);
    // std::this_thread::sleep_for(std::chrono::seconds(10));
    return 0;
}